﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HelloASPX
{
    public partial class order : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Write("<script>alert('请先登陆！');location='Login.aspx'</script>");
            }
            if (Session["ProductId"] == null)
            {
                Response.Write("<script>alert('请先选择您要购买的商品！');location='ProductList.aspx'</script>");
            }


            string username = Session["username"].ToString();
            string pid = Session["ProductId"].ToString();

            XmlDocument doc1 = new XmlDocument();
            doc1.Load(Server.MapPath("~/App_Data/user.xml"));
            XmlNode usernode = doc1.SelectSingleNode("//user[username='" + username + "']");

            XmlDocument doc2 = new XmlDocument();
            doc2.Load(Server.MapPath("~/App_Data/products.xml"));
            XmlNode productnode = doc2.SelectSingleNode("//product[pid='" + pid + "']");

            if (usernode != null)
            {
                Recipient.Text = usernode.SelectSingleNode("username").InnerText;
                telphone.Text = usernode.SelectSingleNode("tel").InnerText;
                
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('没有找到该用户！');location='Login.aspx'</script>");
            }
            if(productnode != null)
            {
                ProductDetailName.Text = productnode.SelectSingleNode("pname").InnerText;
                ProductDetailPrice.Text = productnode.SelectSingleNode("pprice").InnerText;
                ProductDetailTime.Text = productnode.SelectSingleNode("ptime").InnerText;
                ProductDetailInfo.Text = productnode.SelectSingleNode("pinfo").InnerText;
                ProductDetailImage.ImageUrl = productnode.SelectSingleNode("pimage").InnerText;
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('请选择存在的商品！');location='ProductList.aspx'</script>");
            }



        }

        protected void useradminButton_Click(object sender, EventArgs e)
        {
            Session["username"] = Request["username"];
            Response.Redirect("Default.aspx");
        }

        protected void AddProduct_Click(object sender, EventArgs e)
        {
            Session["username"] = Request["username"];
            Response.Redirect("AddProduct.aspx");
        }

        protected void createOrder_Click(object sender, EventArgs e)
        {
            if (Recipient.Text == "" || address.Text == "" || telphone.Text == "")
            {
                Response.Write("<script>alert('请将信息填写完整！');'</script>");
                return;
            }

            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_Data/orders.xml"));
            XmlElement ordernode = doc.CreateElement("order");

            XmlNodeList ordernodes = doc.SelectNodes("//order");

            XmlElement oidnode = doc.CreateElement("oid");
            oidnode.InnerText = ordernodes.Count + 1 + "";
            ordernode.AppendChild(oidnode);

            XmlElement usernamenode = doc.CreateElement("username");
            usernamenode.InnerText = Session["username"].ToString();
            ordernode.AppendChild(usernamenode);

            XmlElement pidnode = doc.CreateElement("pid");
            pidnode.InnerText = Session["ProductId"].ToString();
            ordernode.AppendChild(pidnode);

            XmlElement pnamenode = doc.CreateElement("pname");
            pnamenode.InnerText = ProductDetailName.Text;
            ordernode.AppendChild(pnamenode);

            XmlElement ptimenode = doc.CreateElement("ptime");
            ptimenode.InnerText = ProductDetailTime.Text;
            ordernode.AppendChild(ptimenode);

            XmlElement pimagenode = doc.CreateElement("pimage");
            pimagenode.InnerText = ProductDetailImage.ImageUrl;
            ordernode.AppendChild(pimagenode);

            XmlElement ppricenode = doc.CreateElement("pprice");
            ppricenode.InnerText = ProductDetailPrice.Text;
            ordernode.AppendChild(ppricenode);

            XmlElement pinfonode = doc.CreateElement("pinfo");
            pinfonode.InnerText = ProductDetailInfo.Text;
            ordernode.AppendChild(pinfonode);

            XmlElement recipientnode = doc.CreateElement("recipient");
            recipientnode.InnerText = Recipient.Text;
            ordernode.AppendChild(recipientnode);

            XmlElement addressnode = doc.CreateElement("address");
            addressnode.InnerText = address.Text;
            ordernode.AppendChild(addressnode);

            XmlElement telphonenode = doc.CreateElement("telphone");
            telphonenode.InnerText = telphone.Text;
            ordernode.AppendChild(telphonenode);

            XmlElement orderstatenode = doc.CreateElement("orderstate");
            orderstatenode.InnerText = 1 + "";
            ordernode.AppendChild(orderstatenode);

            doc.DocumentElement.AppendChild(ordernode);
            doc.Save(Server.MapPath("~/App_Data/orders.xml"));

            Response.Write("<script>alert('订单生成成功！');</script>");
        }
    }
}